// 配置Tailwind自定义颜色和字体
tailwind.config = {
    theme: {
        extend: {
            colors: {
                primary: '#165DFF',
                secondary: '#36CFC9',
                accent: '#722ED1',
                neutral: '#F5F7FA',
                dark: '#1D2129',
                success: '#52C41A',
                warning: '#FAAD14',
                danger: '#F5222D'
            },
            fontFamily: {
                inter: ['Inter', 'system-ui', 'sans-serif'],
            },
        }
    }
}

document.addEventListener('DOMContentLoaded', function() {
    // 验证用户登录状态
    const users = JSON.parse(localStorage.getItem('registeredUsers')) || [];
    const loginEmail = localStorage.getItem('currentLoginEmail');
    
    if (!loginEmail) {
        window.location.href = 'index.html';
        return;
    }
    
    // 获取当前用户信息
    const currentUser = users.find(user => user.email === loginEmail);
    if (currentUser) {
        // 更新导航栏用户信息
        document.getElementById('user-display-name').textContent = currentUser.name || '用户';
        if (currentUser.avatar) {
            document.getElementById('header-avatar').src = currentUser.avatar;
        }
    } else {
        localStorage.removeItem('currentLoginEmail');
        window.location.href = 'index.html';
    }
    
    // 移动端菜单切换
    const mobileMenuButton = document.getElementById('mobile-menu-button');
    const mobileMenu = document.getElementById('mobile-menu');
    
    mobileMenuButton.addEventListener('click', function() {
        mobileMenu.classList.toggle('hidden');
    });
    
    // 检查更新
    document.getElementById('check-updates').addEventListener('click', function() {
        const modal = document.getElementById('update-modal');
        const modalContent = document.getElementById('update-modal-content');
        const modalActions = document.getElementById('update-modal-actions');
        
        // 显示加载状态
        modalContent.innerHTML = '<p class="text-gray-600">正在检查更新...</p>';
        modalActions.innerHTML = '<button class="close-modal px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors">确定</button>';
        modal.classList.remove('hidden');
        
        // 模拟检查更新过程
        setTimeout(() => {
            // 随机决定是否有更新（70%概率无更新，30%概率有更新）
            const hasUpdate = Math.random() < 0.3;
            
            if (hasUpdate) {
                document.getElementById('update-modal-title').textContent = '发现新版本';
                modalContent.innerHTML = `
                    <p class="text-gray-600 mb-4">有新版本可用 (v1.1.0)，建议立即更新以获得更好的体验。</p>
                    <div class="bg-gray-50 p-3 rounded-lg">
                        <h5 class="text-sm font-medium mb-2">更新内容：</h5>
                        <ul class="text-sm text-gray-600 space-y-1">
                            <li class="flex items-start">
                                <i class="fa fa-check-circle text-success mt-0.5 mr-2"></i>
                                <span>新增深色模式</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fa fa-check-circle text-success mt-0.5 mr-2"></i>
                                <span>优化任务提醒功能</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fa fa-check-circle text-success mt-0.5 mr-2"></i>
                                <span>修复已知问题</span>
                            </li>
                        </ul>
                    </div>
                `;
                modalActions.innerHTML = `
                    <button class="close-modal px-4 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors mr-3">
                        稍后更新
                    </button>
                    <button id="update-now" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors">
                        立即更新
                    </button>
                `;
                
                // 立即更新按钮事件
                document.getElementById('update-now').addEventListener('click', function() {
                    modalContent.innerHTML = '<p class="text-gray-600">正在更新中...</p>';
                    modalActions.innerHTML = '';
                    
                    // 模拟更新过程
                    setTimeout(() => {
                        modal.classList.add('hidden');
                        showToast('应用已更新到最新版本');
                    }, 2000);
                });
            } else {
                document.getElementById('update-modal-title').textContent = '已是最新版本';
                modalContent.innerHTML = '<p class="text-gray-600">您当前使用的已是最新版本 (v1.0.0)。</p>';
            }
            
            // 重新绑定关闭按钮事件
            document.querySelectorAll('.close-modal').forEach(button => {
                button.addEventListener('click', function() {
                    modal.classList.add('hidden');
                });
            });
        }, 1500);
    });
    
    // 点击模态框外部关闭
    document.getElementById('update-modal').addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.add('hidden');
        }
    });
    
    // 退出登录
    document.getElementById('logout-btn').addEventListener('click', function() {
        if (confirm('确定要退出登录吗？')) {
            localStorage.removeItem('currentLoginEmail');
            window.location.href = 'index.html';
        }
    });
    
    // 显示提示消息
    function showToast(message) {
        const toast = document.getElementById('success-toast');
        document.getElementById('toast-message').textContent = message;
        
        // 显示toast
        toast.classList.remove('translate-y-20', 'opacity-0');
        toast.classList.add('translate-y-0', 'opacity-100');
        
        // 3秒后隐藏
        setTimeout(() => {
            toast.classList.remove('translate-y-0', 'opacity-100');
            toast.classList.add('translate-y-20', 'opacity-0');
        }, 3000);
    }
     // 用户菜单下拉功能
            const userMenuButton = document.getElementById('user-menu-button');
            const userMenuDropdown = document.getElementById('user-menu-dropdown');
            
            // 点击头像切换菜单显示/隐藏
            userMenuButton.addEventListener('click', function(event) {
                event.stopPropagation(); // 防止事件冒泡
                userMenuDropdown.classList.toggle('hidden');
            });
            
            // 点击页面其他地方关闭菜单
            document.addEventListener('click', function(event) {
                if (!userMenuButton.contains(event.target) && !userMenuDropdown.contains(event.target)) {
                    userMenuDropdown.classList.add('hidden');
                }
            });
            
            // 退出登录功能
            document.getElementById('logout-button').addEventListener('click', function() {
                localStorage.removeItem('currentLoginEmail');
                window.location.href = 'index.html';
            });
            
     updateAvatar();
    
    // 监听头像更新事件
    window.addEventListener('avatarUpdated', function(e) {
        const headerAvatar = document.getElementById('header-avatar');
        if (headerAvatar) {
            headerAvatar.src = e.detail.avatar;
        }
    });
    
    // 监听本地存储变化（应对多标签页情况）
    window.addEventListener('storage', function(e) {
        if (e.key === 'registeredUsers') {
            updateAvatar();
        }
    });
});
